#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import re
import htmlentitydefs

html_entity_pattern = re.compile("&(\w+?);")
def html_entity_decode_char(m, defs=htmlentitydefs.entitydefs):
    try:
        return defs[m.group(1)]
    except KeyError:
        return m.group(0)

def html_entity_decode(s):
    return html_entity_pattern.sub(html_entity_decode_char, s)

def strip_whitespace(s, strict=False):
    s = s.replace('\r', '').replace('\n', '').replace('\t', '').replace('\x0B', '')
    return s.strip() if strict else s

def file_get_contents(file) :
    fp = open(file, 'rb')
    content = fp.read()
    fp.close()
    return content

def file_put_contents(file, s, append=False) :
    fp = open(file,'ab' if append else 'wb')
    fp.write(s)
    fp.close()
    return True

def debug(s, char='*', length=100):
    print '\n', char*length ,'\n'
    print s
    print '\n', char*length ,'\n'
